# 逻辑回归实战
from sklearn.datasets import load_breast_cancer
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import classification_report,confusion_matrix
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import cross_validate
data = load_breast_cancer() # 加载数据集
X,y = data.data,data.target # 取得数据特征和标签类别
X = StandardScaler().fit_transform(X) # 数据标准化
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.3,random_state=1) # 切分训练集和测试集
logistic_clf = LogisticRegression() # 模型实例化
logistic_clf.fit(X_train,y_train) #训练模型
acc = logistic_clf.score(X_test,y_test) # 效果评估
print("The accuracy is %.3f"%acc) # 打印平均正确率
y_predit = logistic_clf.predict(X_test) # 进行预测
print(y_predit[:5],y_test[:5]) # 打印预测结果和真实值

print(f"{classification_report(y_test,y_predit)}")
print(confusion_matrix(y_test,y_predit))

# 交叉验证
data = load_breast_cancer() # 加载数据集
X,y = data.data,data.target # 取得数据特征和标签类别
X = StandardScaler().fit_transform(X) # 数据标准化
logistic_clf = LogisticRegression() # 模型实例化
scores = cross_val_score(logistic_clf,X,y,cv=5) # 分为5份，进行5次验证
print(scores,'\n%0.2f accuracy with std %0.2f'%(scores.mean(),scores.std()))
scores = cross_val_score(logistic_clf,X,y,cv=5,scoring='precision_macro') # 分为5份，进行5次验证
print(scores,'\n%0.2f accuracy with std %0.2f'%(scores.mean(),scores.std()))

scoring = ['precision_macro','recall_macro','f1_macro']
scores = cross_validate(logistic_clf,X,y,cv=5,scoring=scoring)
print(scores)